package com.cms.recursoshumanos.utilities;

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.system.Bitmap;

public class ImageLoader 
{
    
	
	public static Bitmap getImageFromUrl(String url) {
        Bitmap bitmap = null;

        try {
            String bitmapData = getDataFromUrl(url);
            bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0,
                    bitmapData.length(), 1);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return bitmap;
    }

    private static String getDataFromUrl(String url) {
        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;

        long len = 0;
        int ch = 0;

        try {
            c = (HttpConnection) Connector.open(url);

            is = c.openInputStream();
            len = c.getLength();
            if (len != -1) {
                for (int i = 0; i < len; i++)
                    if ((ch = is.read()) != -1) {
                        b.append((char) ch);
                    }
            } else {
                while ((ch = is.read()) != -1) {
                    len = is.available();
                    b.append((char) ch);
                }
            }

            is.close();
            c.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return b.toString();
    }
	
}

